-- Menu so that players can buy stuff
haspres = true

function inv_statmenu()
		print ("Stat Menu Called")
	
	
	local StatWindow = vgui.Create( "DFrame" ) -- Creates the frame itself
	StatWindow:SetPos( (ScrW() / 2) - 150 , (ScrH() / 2) - 250 ) -- Position on the players screen
	StatWindow:SetSize( 300, 500) -- Size of the frame
	StatWindow:SetTitle( "Buy and Upgrade" ) -- Title of the frame
	StatWindow:SetVisible( true )
	StatWindow:SetDraggable( false ) -- Draggable by mouse?
	StatWindow:ShowCloseButton( true ) -- Show the close button?
	StatWindow:MakePopup() -- Show the frame
	
	--------------------
	-- UPGRADES PANEL --
	--------------------
	local UpgradeSheet = vgui.Create( "DPanel", StatWindow )
	UpgradeSheet:SetPos( 0, 0)
	UpgradeSheet:SetSize( 250, 250 )
	UpgradeSheet.Paint = function() -- Paint function
		surface.SetDrawColor( 50, 50, 50, 255 ) -- Set our rect color below us; we do this so you can see items added to this panel
		surface.DrawRect( 0, 0, UpgradeSheet:GetWide(), UpgradeSheet:GetTall() ) -- Draw the rect
	end

	--00000000
	--Weapon Speed
	local WeaponSpeedLabel = vgui.Create("DLabel", UpgradeSheet)
	WeaponSpeedLabel:SetPos( 12, 12)
	WeaponSpeedLabel:SetText("Weapon Speed (%)")
	WeaponSpeedLabel:SizeToContents()
	
	local WeaponSpeedValue = vgui.Create("DTextEntry", UpgradeSheet)
	WeaponSpeedValue:SetPos( 170, 8)
	WeaponSpeedValue:SetSize( 64, 24 )
	WeaponSpeedValue:SetText(tostring(LocalPlayer():GetWeaponSpeed()))
	WeaponSpeedValue:SetEditable(false)
	
	local WeaponSpeedButton = vgui.Create("DSysButton", UpgradeSheet)
	WeaponSpeedButton:SetType( "up" )
	WeaponSpeedButton:SetPos( 240, 8)
	WeaponSpeedButton:SetSize( 32, 24 )
	WeaponSpeedButton:SetToolTip("Increases speed of all weapons, Cost: " .. INV_WEAPONSPEEDUPGRADECOST[2] .. " per " .. INV_WEAPONSPEEDUPGRADECOST[1] .. "%")
	WeaponSpeedButton.DoClick = function()
        RunConsoleCommand("inv_upgradeweaponspeed")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_WEAPONSPEEDUPGRADECOST[2] then
			WeaponSpeedValue:SetText(tostring(LocalPlayer():GetWeaponSpeed()) + INV_WEAPONSPEEDUPGRADECOST[1])
		end
    end
	
	--00000000
	--Weapon Damage
	local WeaponDamageLabel = vgui.Create("DLabel", UpgradeSheet)
	WeaponDamageLabel:SetPos( 12, 42)
	WeaponDamageLabel:SetText("Weapon Damage (%)")
	WeaponDamageLabel:SizeToContents()
	
	local WeaponDamageValue = vgui.Create("DTextEntry", UpgradeSheet)
	WeaponDamageValue:SetPos( 170, 38)
	WeaponDamageValue:SetSize( 64, 24 )
	WeaponDamageValue:SetText(tostring(LocalPlayer():GetWeaponDamage()))
	WeaponDamageValue:SetEditable(false)
	
	local WeaponDamageButton = vgui.Create("DSysButton", UpgradeSheet)
	WeaponDamageButton:SetType( "up" )
	WeaponDamageButton:SetPos( 240, 38)
	WeaponDamageButton:SetSize( 32, 24 )
	WeaponDamageButton:SetToolTip("Increases damage caused by all weapons, Cost: " .. INV_WEAPONDAMAGEUPGRADECOST[2] .. " per " .. INV_WEAPONDAMAGEUPGRADECOST[1] .. "%")
	WeaponDamageButton.DoClick = function()
        RunConsoleCommand("inv_upgradeweapondamage")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_WEAPONDAMAGEUPGRADECOST[2] then
			WeaponDamageValue:SetText(tostring(LocalPlayer():GetWeaponDamage()) + INV_WEAPONDAMAGEUPGRADECOST[1])
		end
    end
	
	--00000000
	--Clip Size
	local ClipSizeLabel = vgui.Create("DLabel", UpgradeSheet)
	ClipSizeLabel:SetPos( 12, 72)
	ClipSizeLabel:SetText("Clip Size (%)")
	ClipSizeLabel:SizeToContents()
	
	local ClipSizeValue = vgui.Create("DTextEntry", UpgradeSheet)
	ClipSizeValue:SetPos( 170, 68)
	ClipSizeValue:SetSize( 64, 24 )
	ClipSizeValue:SetText(tostring(LocalPlayer():GetClipSize()))
	ClipSizeValue:SetEditable(false)
	
	local ClipSizeButton = vgui.Create("DSysButton", UpgradeSheet)
	ClipSizeButton:SetType( "up" )
	ClipSizeButton:SetPos( 240, 68)
	ClipSizeButton:SetSize( 32, 24 )
	ClipSizeButton:SetToolTip("Increases the ammo capacity of all weapons, Cost: " .. INV_CLIPSIZEUPGRADECOST[2] .. " per " .. INV_CLIPSIZEUPGRADECOST[1] .. "%")
	ClipSizeButton.DoClick = function()
        RunConsoleCommand("inv_upgradeclipsize")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_CLIPSIZEUPGRADECOST[2] then
			ClipSizeValue:SetText(tostring(LocalPlayer():GetClipSize()) + INV_CLIPSIZEUPGRADECOST[1])
		end
    end
	
	--00000000
	--Damage Taken
	local DamageTakenLabel = vgui.Create("DLabel", UpgradeSheet)
	DamageTakenLabel:SetPos( 12, 102)
	DamageTakenLabel:SetText("Damage Taken (%)")
	DamageTakenLabel:SizeToContents()
	
	local DamageTakenValue = vgui.Create("DTextEntry", UpgradeSheet)
	DamageTakenValue:SetPos( 170, 98)
	DamageTakenValue:SetSize( 64, 24 )
	DamageTakenValue:SetText(tostring(LocalPlayer():GetDamageTaken()))
	DamageTakenValue:SetEditable(false)
	
	local DamageTakenButton = vgui.Create("DSysButton", UpgradeSheet)
	DamageTakenButton:SetType( "up" )
	DamageTakenButton:SetPos( 240, 98)
	DamageTakenButton:SetSize( 32, 24 )
	DamageTakenButton:SetToolTip("Decreases damage taken when hit, Cost: " .. INV_DAMAGETAKENUPGRADECOST[2] .. " per " .. INV_DAMAGETAKENUPGRADECOST[1] .. "%")
	DamageTakenButton.DoClick = function()
        RunConsoleCommand("inv_upgradedamagetaken")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_DAMAGETAKENUPGRADECOST[2] then
			DamageTakenValue:SetText(tostring(LocalPlayer():GetDamageTaken()) + INV_DAMAGETAKENUPGRADECOST[1])
		end
    end
	
	--00000000
	--Max Health
	local MaxHealthLabel = vgui.Create("DLabel", UpgradeSheet)
	MaxHealthLabel:SetPos( 12, 132)
	MaxHealthLabel:SetText("Maximum Health")
	MaxHealthLabel:SizeToContents()
	
	local MaxHealthValue = vgui.Create("DTextEntry", UpgradeSheet)
	MaxHealthValue:SetPos( 170, 128)
	MaxHealthValue:SetSize( 64, 24 )
	MaxHealthValue:SetText(tostring(LocalPlayer():GetMaxHealth()))
	MaxHealthValue:SetEditable(false)
	
	local MaxHealthButton = vgui.Create("DSysButton", UpgradeSheet)
	MaxHealthButton:SetType( "up" )
	MaxHealthButton:SetPos( 240, 128)
	MaxHealthButton:SetSize( 32, 24 )
	MaxHealthButton:SetToolTip("Increases health capacity, Cost: " .. INV_MAXIMUMHEALTHUPGRADECOST[2] .. " per " .. INV_MAXIMUMHEALTHUPGRADECOST[1] .. "%")
	MaxHealthButton.DoClick = function()
        RunConsoleCommand("inv_upgrademaxhealth")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_MAXIMUMHEALTHUPGRADECOST[2] then
			MaxHealthValue:SetText(tostring(LocalPlayer():GetMaxHealth()) + INV_MAXIMUMHEALTHUPGRADECOST[1])
		end
    end
	
	--00000000
	--Speed
	local SpeedLabel = vgui.Create("DLabel", UpgradeSheet)
	SpeedLabel:SetPos( 12, 162)
	SpeedLabel:SetText("Speed (%)")
	SpeedLabel:SizeToContents()
	
	local SpeedValue = vgui.Create("DTextEntry", UpgradeSheet)
	SpeedValue:SetPos( 170, 158)
	SpeedValue:SetSize( 64, 24 )
	SpeedValue:SetText(tostring(LocalPlayer():GetSpeed()))
	SpeedValue:SetEditable(false)
	
	local SpeedButton = vgui.Create("DSysButton", UpgradeSheet)
	SpeedButton:SetType( "up" )
	SpeedButton:SetPos( 240, 158)
	SpeedButton:SetSize( 32, 24 )
	SpeedButton:SetToolTip("Increases running speed, Cost: " .. INV_SPEEDUPGRADECOST[2] .. " per " .. INV_SPEEDUPGRADECOST[1] .. "%")
	SpeedButton.DoClick = function()
        RunConsoleCommand("inv_upgradespeed")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_SPEEDUPGRADECOST[2] then
			SpeedValue:SetText(tostring(LocalPlayer():GetSpeed()) + INV_SPEEDUPGRADECOST[1])
		end
    end

	--00000000
	--Speed
	local JumpLabel = vgui.Create("DLabel", UpgradeSheet)
	JumpLabel:SetPos( 12, 192)
	JumpLabel:SetText("Jump (%)")
	JumpLabel:SizeToContents()
	
	local JumpValue = vgui.Create("DTextEntry", UpgradeSheet)
	JumpValue:SetPos( 170, 188)
	JumpValue:SetSize( 64, 24 )
	JumpValue:SetText(tostring(LocalPlayer():GetJump()))
	JumpValue:SetEditable(false)
	
	local JumpButton = vgui.Create("DSysButton", UpgradeSheet)
	JumpButton:SetType( "up" )
	JumpButton:SetPos( 240, 188)
	JumpButton:SetSize( 32, 24 )
	JumpButton:SetToolTip("Increases jump height, Cost: " .. INV_JUMPUPGRADECOST[2] .. " per " .. INV_JUMPUPGRADECOST[1] .. "%")
	JumpButton.DoClick = function()
        RunConsoleCommand("inv_upgradejump")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_JUMPUPGRADECOST[2] then
			JumpValue:SetText(tostring(LocalPlayer():GetJump()) + INV_JUMPUPGRADECOST[1])
		end
    end
	
	--00000000
	--WeaponSpread
	local SpreadLabel = vgui.Create("DLabel", UpgradeSheet)
	SpreadLabel:SetPos( 12, 222)
	SpreadLabel:SetText("Weapon Spread (%)")
	SpreadLabel:SizeToContents()
	
	local SpreadValue = vgui.Create("DTextEntry", UpgradeSheet)
	SpreadValue:SetPos( 170, 218)
	SpreadValue:SetSize( 64, 24 )
	SpreadValue:SetText(tostring(LocalPlayer():GetSpread()))
	SpreadValue:SetEditable(false)
	
	local SpreadButton = vgui.Create("DSysButton", UpgradeSheet)
	SpreadButton:SetType( "up" )
	SpreadButton:SetPos( 240, 218)
	SpreadButton:SetSize( 32, 24 )
	SpreadButton:SetToolTip("Decrease Weapon Spread, Cost: " .. INV_SPREADUPGRADECOST[2] .. " per " .. INV_SPREADUPGRADECOST[1] .. "%")
	SpreadButton.DoClick = function()
        RunConsoleCommand("inv_upgradespread")
		-- Bit of a hack to update the UI with the changes
		if LocalPlayer():GetStatpoints() >= INV_SPREADUPGRADECOST[2] then
			SpreadValue:SetText(tostring(LocalPlayer():GetSpread()) + INV_SPREADUPGRADECOST[1])
		end
    end

	
	--00000000
	--Stat readout
	--local StatPointsLabel = vgui.Create("DLabel", UpgradeSheet)
	--StatPointsLabel:SetPos( 12, 192)
	--StatPointsLabel:SetText("You have " .. tostring(LocalPlayer():GetStatpoints()) .. " Stat Points left to spend!")
	--StatPointsLabel:SizeToContents()
	
	-- Upgrades
	--[[local SkillsList = vgui.Create("DListView")
	SkillsList:SetParent(UpgradeSheet)
	SkillsList:SetPos(5, 222)
	SkillsList:SetSize(270, 200)
	SkillsList:SetMultiSelect(false)
	SkillsList:AddColumn("Skill") -- Add column
	SkillsList:AddColumn("Cost")
	SkillsList.DoDoubleClick = function(parent, index, list)
					RunConsoleCommand("inv_upgrade", list:GetValue(3))
				end
	SkillsList:AddLine("test","test")
	SkillsList:AddLine("test","test")
	SkillsList:AddLine("test","test")
	SkillsList:AddLine("test","test")
	SkillsList:AddLine("test","test")
	SkillsList:AddLine("test","test")]]--

	--------------------
	-- Buyables Panel --
	--------------------
	local BuyablesSheet = vgui.Create( "DPanel", StatWindow )
	BuyablesSheet:SetPos( 0,0 )
	BuyablesSheet.Paint = function() -- Paint function
		surface.SetDrawColor( 60, 60, 60, 255 ) -- Set our rect color below us; we do this so you can see items added to this panel
		surface.DrawRect( 0, 0, BuyablesSheet:GetWide(), BuyablesSheet:GetTall() ) -- Draw the rect
	end
	--PropSheetPanel2:SetSize( 250, 250 )
	-- Upgrades
	local BuyablesList = vgui.Create("DListView")
	BuyablesList:SetParent(BuyablesSheet)
	BuyablesList:SetPos(5, 5)
	BuyablesList:SetSize(270, 280)
	BuyablesList:SetMultiSelect(false)
	BuyablesList:AddColumn("Buyable") -- Add column
	BuyablesList:AddColumn("Cost")
	
	local alreadyownedlbl = vgui.Create("DLabel", BuyablesSheet)
	alreadyownedlbl:SetPos( 5, 290)
	alreadyownedlbl:SetText("Already Owned:")
	alreadyownedlbl:SizeToContents()
	
	-- AlreadyOwnedUpgrades
	local OwnedBuyablesList = vgui.Create("DListView")
	OwnedBuyablesList:SetParent(BuyablesSheet)
	OwnedBuyablesList:SetPos(5, 310)
	OwnedBuyablesList:SetSize(270, 115)
	OwnedBuyablesList:SetMultiSelect(false)
	OwnedBuyablesList:AddColumn("Buyable") -- Add column
	OwnedBuyablesList:AddColumn("Cost")
	
	-- Get what the player already owns
	local playerownedbuyables = glon.decode(LocalPlayer():GetOwnedBuyables())
	
	for k, v in pairs(playerownedbuyables) do
		local line = OwnedBuyablesList:AddLine(INV_BUYABLES[v][1],INV_BUYABLES[v][2])
		line:SetToolTip(INV_BUYABLES[v][4])
	end
	
	-- Add all buyables...
	for i=1, table.Count(INV_BUYABLES) do
		-- Except those already owned by the player, or ones where they don't own a prerequist
		if table.HasValue(playerownedbuyables, i) == false || INV_BUYABLES[i][6] == false then
			-- Player doesn't own it, now check pres
			haspres = true
			-- Does this buyable even have pres?
			if INV_BUYABLES[i][7] != nil then
				-- Go through each pre
				for k, v in pairs(INV_BUYABLES[i][7]) do
					-- Check if player has it
					-- print(v)
					if table.HasValue(playerownedbuyables, v) == false then
						
						haspres = false
					end
				end
			end

			if haspres == true then
				local line = BuyablesList:AddLine(INV_BUYABLES[i][1],INV_BUYABLES[i][2], i)
				line:SetToolTip(INV_BUYABLES[i][4])
			end
		end
	end
	
	BuyablesList.DoDoubleClick = function(parent, index, line)
					RunConsoleCommand("inv_buy",line:GetValue(3))
					local ID = math.floor(line:GetValue(3))
					if(INV_BUYABLES[ID][6]) then -- If Buyonce is true, THEN we get rid of it...
						parent:RemoveLine(index) -- Remove it from the list
						local ownedline = OwnedBuyablesList:AddLine(INV_BUYABLES[ID][1],INV_BUYABLES[ID][2])
						ownedline:SetToolTip(INV_BUYABLES[ID][4])
					end
				end

	
	local PropertySheet = vgui.Create( "DPropertySheet" )
	PropertySheet:SetParent( StatWindow )
	PropertySheet:SetPos( 5, 30 )
	PropertySheet:SetSize( 290, 465 )
	PropertySheet:AddSheet( "Upgrades", UpgradeSheet, "gui/silkicons/brick_add", false, false, "Increase weapon speed and such" )
	PropertySheet:AddSheet( "Buyables", BuyablesSheet, "gui/silkicons/add", false, false, "GEt a new weapon, Earn a masters" )

end
concommand.Add("inv_statmenu", inv_statmenu)

function GM:OnSpawnMenuOpen( ply )
	LocalPlayer():ConCommand("inv_statmenu")
end

function CheckAndRemovePres(ID, playerbuyables)
	if INV_BUYABLES[ID][7] != nil then
		-- Go through each pre
		for k, v in pairs(INV_BUYABLES[ID][7]) do
			-- Check if player has it
			-- print(v)
			CheckAndRemovePres(k, playerbuyables)
			
			if table.HasValue(playerbuyables, v) == false then
				haspres = false
			end
		end
	end
end